
package com.hlkj.pay.controller.admin;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.framework.common.pojo.PageResult;
import com.hlkj.framework.common.util.collection.CollectionUtils;
import com.hlkj.pay.app.adminuser.IAdminTeamsAppService;
import com.hlkj.pay.controller.BaseController;
import com.hlkj.pay.dto.admin.AdminTeamsQueryDto;
import com.hlkj.pay.infrastructure.model.admin.AdminTeamsDO;
import com.hlkj.pay.infrastructure.model.admin.AdminUserDO;
import com.hlkj.pay.service.admin.IAdminTeamsService;
import com.hlkj.pay.service.admin.IAdminUserService;
import com.hlkj.pay.vo.admin.req.teams.AdminTeamsPageReq;
import com.hlkj.pay.vo.admin.req.teams.AdminTeamsReq;
import com.hlkj.pay.vo.admin.req.teams.TeamsUserReq;
import com.hlkj.pay.vo.admin.resp.teams.AdminTeamsResp;
import com.hlkj.pay.vo.common.req.DeleteCodeReq;

import cn.hutool.core.bean.BeanUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author HlpayTeam
 * @date 2024/11/05 14:34
 */
@Tag(name = "团队管理")
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/v1/adminuser/teams")
public class AdminTeamsController extends BaseController {

    private final IAdminTeamsService adminTeamsService;

    private final IAdminUserService adminUserService;

    private final IAdminTeamsAppService adminTeamsAppService;

    @Operation(summary = "page团队列表")
    @GetMapping("/page")
    public CommonResult<PageResult<AdminTeamsResp>> queryTeamsPage(AdminTeamsPageReq adminTeamsPageReq) {
        AdminTeamsQueryDto adminTeamsQueryDto = new AdminTeamsQueryDto();
        adminTeamsQueryDto.setLikeName(adminTeamsPageReq.getName());
        adminTeamsQueryDto.setLikeCode(adminTeamsPageReq.getCode());
        adminTeamsQueryDto.setPageNo(adminTeamsPageReq.getPageNo());
        adminTeamsQueryDto.setPageSize(adminTeamsPageReq.getPageSize());
        adminTeamsQueryDto.setSysType(adminTeamsPageReq.getSysType());
        adminTeamsQueryDto.setSn(adminTeamsPageReq.getSn());
        PageResult<AdminTeamsDO> pageResult = adminTeamsService.queryPage(adminTeamsQueryDto);
        List<AdminTeamsDO> list = pageResult.getList();
        if (CollectionUtils.isEmpty(list)) {
            return buildPageResult(pageResult, Collections.emptyList());
        }
        List<AdminTeamsResp> adminTeamsResps = BeanUtil.copyToList(list, AdminTeamsResp.class);
        processMerchantList(adminTeamsResps);
        List<Long> ownIds = adminTeamsResps.stream().map(adminTeamsResp -> adminTeamsResp.getOwnId()).collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(ownIds)) {
            List<AdminUserDO> adminUserList = adminUserService.queryUserList(ownIds);
            Map<Long, AdminUserDO> adminUserMap = adminUserList.stream().collect(Collectors.toMap(adminUserDO -> adminUserDO.getId(), Function.identity(), (t1, t2) -> t1));
            adminTeamsResps.stream().forEach(adminTeamsResp -> {
                if (adminTeamsResp.getOwnId() != null) {
                    AdminUserDO adminUserDO = adminUserMap.get(adminTeamsResp.getOwnId());
                    if (adminUserDO != null) {
                        adminTeamsResp.setOwnUserName(adminUserDO.getUserName());
                    }
                }
            });
        }
        return buildPageResult(pageResult, adminTeamsResps);
    }

    @Operation(summary = "团队列表")
    @GetMapping("")
    public CommonResult<List<AdminTeamsResp>> queryTeamsList(AdminTeamsPageReq adminTeamsPageReq) {
        AdminTeamsQueryDto adminTeamsQueryDto = new AdminTeamsQueryDto();
        adminTeamsQueryDto.setLikeName(adminTeamsPageReq.getName());
        adminTeamsQueryDto.setLikeCode(adminTeamsPageReq.getCode());
        adminTeamsQueryDto.setPageNo(adminTeamsPageReq.getPageNo());
        List<AdminTeamsDO> list = adminTeamsService.queryList(adminTeamsQueryDto);
        if (CollectionUtils.isEmpty(list)) {
            return CommonResult.success(Collections.emptyList());
        }
        List<AdminTeamsResp> adminTeamsResps = BeanUtil.copyToList(list, AdminTeamsResp.class);
        return CommonResult.success(adminTeamsResps);
    }

    @Operation(summary = "新增团队")
    @PostMapping("")
    public CommonResult<String> addTeams(@Validated @RequestBody AdminTeamsReq adminTeamsReq) {
        return adminTeamsAppService.addTeams(adminTeamsReq);
    }

    @Operation(summary = "更新团队")
    @PutMapping("")
    CommonResult<Void> updateTeams(@Validated @RequestBody AdminTeamsReq adminTeamsReq) {
        return adminTeamsAppService.updateTeams(adminTeamsReq);
    }

    @Operation(summary = "删除团队")
    @DeleteMapping("")
    CommonResult<Void> deleteTeams(@RequestBody DeleteCodeReq deleteCodeReq) {
        return adminTeamsAppService.deleteTeams(deleteCodeReq.getCode());
    }

    @Operation(summary = "新增团队对应的用户")
    @PostMapping("/user")
    CommonResult<Void> assignTeamsUser(@Validated @RequestBody TeamsUserReq teamsUserReq) {
        return adminTeamsAppService.assignTeamsUser(teamsUserReq);
    }

    @Operation(summary = "移除团队对应的用户")
    @PutMapping("/user")
    CommonResult<Void> removeTeamsUser(@Validated @RequestBody TeamsUserReq teamsUserReq) {
        return adminTeamsAppService.removeTeamsUser(teamsUserReq);
    }
}
